<template>
	<view>
		<!-- 搜索框 -->
		<view class="search-box">
			<my-search @click="gotoSearch"></my-search>
		</view>

		<!-- 渲染轮播图 -->
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
			<swiper-item v-for="(item, i) in swiperList" :key="i">
				<navigator class="swiper-item" :url="`/subpkg/goods_detail/goods_detail?goods_id=${item.goods_id}`">
					<image :src="item.image_src"></image>
				</navigator>
			</swiper-item>
		</swiper>

		<!-- 菜单导航 -->
		<view class="nav-list">
			<view class="nav-item" v-for="(item, i) in navList" :key="i" @click="navClickHandler(item)">
				<image :src="item.image_src"></image>
			</view>
		</view>


		<!-- 楼层导航 -->
		<view class="floor-list">
			<view class="floor-item" v-for="(item, i) in floorList" :key="i">
				<!-- 楼层标题 -->
				<image class="floor-title" :src="item.floor_title.image_src" mode="widthFix"></image>


				<!-- 楼层导航图片 -->
				<view class="floor-img-box">

					<!-- 左侧大图 -->
					<navigator class="left-img-box" :url="item.product_list[0].url">
						<image :src="item.product_list[0].image_src"
							:style="{width: item.product_list[0].image_width + 'rpx'}" mode="widthFix"></image>
					</navigator>

					<!-- 右侧四张小图 -->
					<view class="right-img-box">
						<navigator class="right-img-item" v-for="(item2, i2) in item.product_list" :key="i2"
							v-if="i2 !== 0" :url="item2.url">
							<image :src="item2.image_src" :style="{width: item2.image_width + 'rpx'}" mode="widthFix">
							</image>
						</navigator>
					</view>

				</view>



			</view>
		</view>


	</view>
</template>

<script>
	export default {
		data() {
			return {

				// 轮播图数据
				swiperList: [],
				// menu 数据
				navList: [],
				// 楼层数据
				floorList: [],

			};
		},
		onLoad() {
			// 获取轮播图数据
			this.getSwiperList()
			// 获取导航菜单
			this.getNavList()
			// 获取楼层数据
			this.getFloorList()
		},
		methods: {
			// 获取轮播图数据
			async getSwiperList() {
				// 发起网络请求
				const {
					data: res
				} = await uni.$http.get('/home/swiperdata')
				console.log('轮播图', res)

				// 判断数据请求状态
				if (res.meta.status !== 200) return uni.$showMsg()

				// 请求成功，设置页面数据
				this.swiperList = res.message

			},

			// 获取导航菜单数据
			async getNavList() {
				// 发起网络请求
				const {
					data: res
				} = await uni.$http.get('/home/catitems')
				console.log('res', res)
				// 判断请求结果状态
				if (res.meta.status !== 200) return uni.$showMsg()

				// 请求成功，设置页面数据
				this.navList = res.message
			},

			// 获取楼层数据
			async getFloorList() {
				// 发起请求
				const {
					data: res
				} = await uni.$http.get('/home/floordata')
				// 判断请求结果状态
				if (res.meta.status !== 200) return uni.$showMsg()
				// 设置页面数据
				res.message.forEach((floor) => {
					floor.product_list.forEach((prod) => {
						prod.url = '/subpkg/goods_list/goods_list?' + prod.navigator_url.split('?')[1]
					})
				})
				this.floorList = res.message
			},



			// 页面的事件处理
			navClickHandler(item) {
				if (item.name === '分类') {
					uni.switchTab({
						url: '/pages/cate/cate'
					})
				}
			},

			gotoSearch(e) {
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
			}


		}
	}
</script>

<style lang="scss">
	.search-box {
		// 设置定位效果为“吸顶”
		position: sticky;
		// 吸顶的“位置”
		top: 0;
		// 提高层级，防止被轮播图覆盖
		z-index: 9;
	}

	swiper {
		height: 330rpx;

		.swiper-item,
		image {
			height: 100%;
			width: 100%;
		}
	}

	.nav-list {
		display: flex;
		justify-content: space-around;
		padding-top: 32rpx;

		.nav-item {
			image {
				width: 128rpx;
				height: 140rpx;
			}
		}
	}

	.floor-list {
		.floor-item {

			.floor-title {
				height: 60rpx;
				width: 100%;
			}

			.floor-img-box {
				display: flex;

				.left-img-box {
					margin-left: 10rpx;
				}

				.right-img-box {
					display: flex;
					flex-wrap: wrap;
					justify-content: space-around;
				}
			}
		}
	}
</style>
